//
//  TMGGoldAssetViewController.m
//  TMGold
//
//  Created by Mac on 2017/12/12.
//  Copyright © 2017年 tangmu. All rights reserved.
//

#import "TMGGoldAssetViewController.h"
#import "TMMenuControl.h"
#import "TMGSaleGoldViewController.h"
#import "TMGFetchGoldViewController.h"
#import "TMGPlanRecordViewController.h"
#import "TMGFLowGoldRecordViewController.h"
@interface TMGGoldAssetViewController ()
@property(nonatomic, strong) STLabel                     *titleLable;
@property(nonatomic, strong) TMMenuControl                     *huangjinControl;
@property(nonatomic, strong) TMMenuControl                     *histroyControl;
@property(nonatomic, strong) NSArray   <NSArray*>                  *dataSouce;

@property(nonatomic, strong) NSArray                     *originArray;
@end

@implementation TMGGoldAssetViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configSubView];
    [self configDataSouceWithArray:nil];
    [self sendGoldAssetRequest];
    [self configBottomView];
    // Do any additional setup after loading the view.
}

#pragma mark --subView
- (void)configSubView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    header.backgroundColor = [UIColor whiteColor];
    
    UIImageView * bannerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 193)];
    bannerImageView.image = [UIImage imageNamed:@"wdebanner"];
    bannerImageView.userInteractionEnabled = YES;
    [header addSubview:bannerImageView];
    
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(0, 10, 200, 80)
                                                     text:@"黄金资产(元)\n266.1"
                                                textColor:[UIColor whiteColor]
                                                     font:26
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentCenter];
    titleLable.centerX = UIScreenWidth / 2;
    self.titleLable = titleLable;
    [bannerImageView addSubview:titleLable];
    
    self.huangjinControl = [[TMMenuControl alloc] initWithFrame:CGRectMake(0, bannerImageView.bottom, UIScreenWidth , 60) value:@"0.00" name:@"黄金现值(元)" showLine:YES];
    self.huangjinControl.valueLable.textColor = TM_orangeColor;
    [header addSubview:self.huangjinControl];
    
    self.histroyControl = [[TMMenuControl alloc] initWithFrame:CGRectMake(self.huangjinControl.right, bannerImageView.bottom, UIScreenWidth / 2, 60) value:@"0.00" name:@"历史盈亏(元)" showLine:YES];
    self.histroyControl.valueLable.textColor = TM_orangeColor;
   // [header addSubview:self.histroyControl];
     header.height = self.histroyControl.bottom;
    //头部橘色
    UIView * headers = [[UIView alloc] initWithFrame:CGRectMake(0, -header.height, UIScreenWidth, 2 * header.height)];
    headers.backgroundColor = TM_orangeColor;
    [header insertSubview:headers atIndex:0];
    
   
    self.tableView.tableHeaderView = header;
    

}
- (void)configBottomView{
    UIView * bootomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 44)];
    bootomView.bottom = UIScreenHeight;
    [bootomView st_showTopShadow];
    [self.view addSubview:bootomView];
    
    __weak typeof(self) weakSelf =  self;
    self.tableView.height = self.tableView.height - 44;
    STButton * outButton = [[STButton alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth/2, 44)
                                                     title:@"卖出"
                                                titleColor:TM_orangeColor
                                                 titleFont:16
                                              cornerRadius:0
                                           backgroundColor:[UIColor whiteColor]
                                           backgroundImage:nil
                                                     image:nil];
    outButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [outButton st_showRightLine:outButton.height];
    [outButton setClicAction:^(UIButton *sender) {
        [weakSelf onSlectedSaleGoldButton];
    }];
    [bootomView addSubview:outButton];
    
    STButton * tijintButton = [[STButton alloc] initWithFrame:CGRectMake(outButton.right, 0, UIScreenWidth/2, 44)
                                                     title:@"提金"
                                                titleColor:TM_orangeColor
                                                 titleFont:16
                                              cornerRadius:0
                                           backgroundColor:[UIColor whiteColor]
                                           backgroundImage:nil
                                                     image:nil];
    tijintButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [tijintButton setClicAction:^(UIButton *sender) {
        [weakSelf onSlectedTijinButton];
    }];
    [bootomView addSubview:tijintButton];
}
- (void)configDataSouceWithArray:(NSArray*)array{
    
    if (!array.count) {
        NSArray * sectionOne = @[
                                 [[STTableEasyModel alloc] initWithTextString:@"买入均价(元/克)" detailString:@"0.00"],
                                 [[STTableEasyModel alloc] initWithTextString:@"浮动盈亏(元)" detailString:@"0.00"],
                                 [[STTableEasyModel alloc] initWithTextString:@"累计收益(元)" detailString:@"0.00"]];
        self.dataSouce = @[sectionOne,@[[[STTableEasyModel alloc] initWithTextString:@"买入均价(元/克)" detailString:@"0.00"]]];
        [self.tableView reloadData];
    }else{
        NSDictionary * koudai = array[1];
        NSDictionary * xiangdi = array.lastObject;
        
        NSString * average = [self numberWithString:[koudai[@"average"] description]];
        NSString * floatValue = [self numberWithString:[koudai[@"float"] description]];
        NSString * revenue = [self numberWithString:[koudai[@"revenue"] description]];
        NSString * average1 = [self numberWithString:[koudai[@"average"] description]];
        NSArray * sectionOne = @[
                                 [[STTableEasyModel alloc] initWithTextString:@"买入均价(元/克)"
                                                                 detailString:average],
                                 [[STTableEasyModel alloc] initWithTextString:@"浮动盈亏(元)"
                                                                 detailString:floatValue],
                                 [[STTableEasyModel alloc] initWithTextString:@"累计收益(元)"
                                                                 detailString:revenue]];
        self.dataSouce = @[sectionOne,@[[[STTableEasyModel alloc] initWithTextString:@"买入均价(元/克)"
                                                                        detailString:average1]]];
        [self.tableView reloadData];
    }

    
}
- (NSString*)numberWithString:(NSString*)str{
    NSString * finsh = [NSString stringWithFormat:@"%0.2f",str.floatValue];
    return finsh;
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    //59 yincnag 口袋金压箱斤
    return 0;
    return self.dataSouce.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSouce[section].count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 70;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * sectionheader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 70)];
    NSString * title =  @"流动金(克)";
    if (section == 1) {
        title = @"箱底(克)";
    }
    if (self.originArray) {
        if (section == 0) {
            NSDictionary * dic = self.originArray[1];
            title = [NSString stringWithFormat:@"%@(克)",dic[@"name"]];
        }
        if (section == 1) {
            NSDictionary * dic = self.originArray.lastObject;
            title = [NSString stringWithFormat:@"%@(克)",dic[@"name"]];
        }
    }
    NSString * value=  @"0.00";
    if (self.originArray) {
        if (section == 0) {
            NSDictionary * dic = self.originArray[1];
            value = [NSString stringWithFormat:@"%@",dic[@"assets"]];
        }
        if (section == 1) {
            NSDictionary * dic = self.originArray.lastObject;
            value = [NSString stringWithFormat:@"%@",dic[@"assets"]];
        }
    }
    
    __weak typeof(self) weakSelf =  self;
    sectionheader.backgroundColor = self.tableView.backgroundColor;
    STButton * titleLable = [[STButton alloc] initWithFrame:CGRectMake(0, 10, UIScreenWidth, 60)
                                                     title:value
                                                titleColor:TM_orangeColor
                                                 titleFont:22
                                              cornerRadius:0
                                           backgroundColor:[UIColor whiteColor]
                                           backgroundImage:nil
                                                     image:nil];
    
    titleLable.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [titleLable setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedSectionHeaderWithSection:section];
    }];
    [sectionheader addSubview:titleLable];
    
    STLabel * nameLable = [[STLabel alloc] initWithFrame:CGRectMake(self.insetX, 5, 130, 20)
                                                     text:title
                                                textColor:TM_firstTextColor
                                                     font:15
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [titleLable addSubview:nameLable];
    return sectionheader;
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
        
    }
    cell.textLabel.textColor = UIColorFromRGBA(0x999999);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = UIColorFromRGBA(0x333333);
    
    STTableEasyModel * model = self.dataSouce[indexPath.section][indexPath.row];
    cell.textLabel.text = model.textString;
    cell.detailTextLabel.text = model.detailString;
    cell.accessoryType = UITableViewCellAccessoryNone;
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)onSlectedSaleGoldButton{
    [self.navigationController pushViewController:[TMGSaleGoldViewController new] animated:YES];
}
- (void)onSlectedTijinButton{
    [self.navigationController pushViewController:[TMGFetchGoldViewController new] animated:YES];
}
- (void)onSelctedSectionHeaderWithSection:(NSInteger)section{
    if (section == 1) {
        //箱底金
        TMGPlanRecordViewController * vc = [[TMGPlanRecordViewController alloc] init];
        vc.type = @"3";
        [self.navigationController pushViewController:vc animated:YES];
    }
    if (section == 0) {
        //流动
        TMGFLowGoldRecordViewController * vc = [[TMGFLowGoldRecordViewController alloc] init];
       // vc.type = @"2";
        [self.navigationController pushViewController:vc animated:YES];
    }
}
#pragma mark --NetWork Method
- (void)sendGoldAssetRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/personal/goldAssets"];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSArray * array = responseObject[@"data"];
                                                 NSDictionary * firstDic = array.firstObject;
                                                 if ([[firstDic[@"id"] description] isEqualToString:@"1"]) {
                                                     //头部
                                                     NSString * weight = firstDic[@"assets"];
                                                     self.titleLable.text = [NSString stringWithFormat:@"黄金资产(元)\n%@",weight];

                                                     NSString * now = firstDic[@"now"];
                                                     self.huangjinControl.valueLable.text = [NSString stringWithFormat:@"%@",now];
                                                     
                                                     NSString * history = firstDic[@"history"];
                                                     self.histroyControl.valueLable.text = [NSString stringWithFormat:@"%@",history];
                                                 }
                                                 self.originArray = array;
                                                 [self configDataSouceWithArray:array];
                                                 [self.tableView.mj_header endRefreshing];
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 [self.tableView.mj_header endRefreshing];
                                             }];
}
@end
